/**
 * 订单管理-订单审核
 * 
 * @author yellow
 */
var orderConfirmingMask = new Ext.LoadMask(Ext.getBody(), {
			msg : "正在提交,请稍后..."
		});
var queryOrderVfCheckSearch = function() {
	var queryOrdersToPaidFieldSet = new Ext.form.FieldSet({
		id : 'queryOrdersToPaidFieldSet',
		title : '查询条件',
		collapsible : true,// 可折叠
		buttonAlign : 'center',
		keys : [{
					key : 13, // 13代表回车
					fn : queryOrdersToPaid,
					scope : this
				}],
		items : [{
					layout : 'column',
					border : false,
					items : [{
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											xtype : 'textfield',
											name : 'id',
											anchor : '100%',
											fieldLabel : '订单编号'
										}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											id : "orderToPaid_Code",
											xtype : 'textfield',
											anchor : '100%',
											fieldLabel : '订单编码',
											name : "orderCode"
										}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											xtype : 'textfield',
											anchor : '100%',
											fieldLabel : '物流公司电话',
											name : "shipperPhone"
										}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											xtype : 'dataDictionaryCmb',
											anchor : '100%',
											fieldLabel : "支付方式",
											editable : false,
											autoLoad : false,
											existall : true,
											defaultFirstRow : 1,
											hiddenName : 'payType',
											dictionaryType : PmsUtil.orders_vf_pay_type

										}]
							}]
				}, {
					layout : 'column',
					border : false,
					items : [{
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											xtype : 'dataDictionaryCmb',
											anchor : '100%',
											fieldLabel : "支付状态",
											editable : false,
											autoLoad : false,
											existall : true,
											defaultFirstRow : 1,
											hiddenName : 'payStatus',
											dictionaryType : PmsUtil.orders_pay_status
										}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											xtype : 'textfield',
											anchor : '100%',
											fieldLabel : '物流公司名称',
											name : "shipperCodeName"
										}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
									layout : 'column',
									border : false,
									items : [{
												text : '查询',
												xtype : 'button',
												width : 70,
												handler : function() {
													queryOrdersToPaid();
												},
												style : {
													marginLeft : "50px",
													marginRight : "10px"
												}
											}, {
												text : '重置',
												xtype : 'button',
												width : 70,
												handler : function() {
													queryOrderVfCheckForm
															.getForm().reset();
												}
											}]
								}]

							}]
				}]
	});
	fieldSetListeners(['queryOrdersToPaidFieldSet', 'queryOrderVfCheckForm',
					'orderVfManagerGrid'], [0.16, 0.84]);

	var queryOrderVfCheckForm = new Ext.form.FormPanel({
				id : 'queryOrderVfCheckForm',
				anchor : '100% 16%',
				frame : true,
				labelAlign : 'right',
				bodyBorder : false,
				items : [queryOrdersToPaidFieldSet]
			});
	return queryOrderVfCheckForm;
};

/**
 * grids
 */

var orderToPaidManagerGrid = function() {
	var orderVfManagerSm = new Ext.grid.CheckboxSelectionModel({
			});// 创建选择模式对象
	// 定义表的每列（对应表头信息）
	var orderVfManagerCm = new Ext.grid.ColumnModel([orderVfManagerSm, {
				dataIndex : "id",
				header : "订单编号",
				sortable : false,
				width : 60
			}, {
				dataIndex : "orderCode",
				header : "订单编码",
				sortable : false,
				width : 160,
				renderer : function(value, cellmeta, record, rowIndex,
						columnIndex, store) {
					var returnValue = "";
					var orderId = record.data.id;
					if (value) {
						returnValue = '<a href ="#" onClick="lookOrderVfDetail(\''
								+ orderId + '\')">' + value + '</a>';
					}
					return returnValue;
				}
			}, {
				dataIndex : "userId",
				header : "用户编号",
				sortable : false,
				width : 70
			}, {
				dataIndex : "userName",
				header : "用户姓名",
				sortable : false,
				width : 60
			}, {
				dataIndex : "orderType",
				header : "订单类型",
				sortable : false,
				width : 70,
				renderer : function(value) {
					return PmsUtil.OrderType(value);
				}
			}, {
				dataIndex : "payStatus",
				header : "支付状态",
				sortable : false,
				width : 60,
				renderer : function(value) {
					return PmsUtil.OrderPayStatus(value);
				}
			}, {
				dataIndex : "orderStatus",
				header : "订单状态",
				sortable : false,
				width : 60,
				renderer : function(value) {
						return '<font color=\"#FF0000\" >'
								+ PmsUtil.OrderVfStatus(value) + '</font>';
				}
			},  {
				dataIndex : "goodsNumber",
				header : "商品数量",
				sortable : false,
				width : 60
			}, {
				dataIndex : "totalAmount",
				header : "订单总金额",
				sortable : false,
				width : 80,
				renderer : function(value) {
					return Ext.util.Format.number(value, '0,0.00');
				}
			}, {
				dataIndex : "payAmount",
				header : "实际支付金额",
				sortable : false,
				width : 100,
				renderer : function(value) {
					return PmsUtil.Price(value);
				}
			}, {
				dataIndex : "payType",
				header : "支付方式",
				sortable : false,
				width : 60,
				renderer : function(value) {
					return PmsUtil.VfPayType(value);

				}
			}, {
				dataIndex : "shipperCode",
				header : "物流公司编码",
				sortable : false,
				width : 100
			}, {
				dataIndex : "shipperCodeName",
				header : "物流公司名称",
				sortable : false,
				width : 100
			}, {
				dataIndex : "shipperPhone",
				header : "物流公司电话",
				sortable : false,
				width : 100
			}, {
				dataIndex : "consignee",
				header : "收货人",
				sortable : false,
				width : 60
			}, {
				dataIndex : "consignPhone",
				header : "收货人电话",
				sortable : false,
				width : 100
			}, {
				dataIndex : "consignAddress",
				header : "收货人地址",
				sortable : false,
				width : 150,
				renderer : function(value, meta, record) {
					if (value) {
						meta.attr = 'ext:qtitle="收货人地址:" ext:qtip="' + value
								+ '"';
						return value;
					}
				}
			}, {
				dataIndex : "remark",
				header : "备注",
				sortable : false,
				width : 100,
				renderer : function(value, meta, record) {
					if (value) {
						meta.attr = 'ext:qtitle="备注:" ext:qtip="' + value + '"';
						return value;
					}
				}
			}, {
				dataIndex : "createdDate",
				header : "创建时间",
				sortable : false,
				width : 130
			}, {
				dataIndex : "createdName",
				header : "创建人",
				sortable : false,
				width : 60
			}, {
				header : "管理",
				width : 300,
				dataIndex : "id",
				renderer : function(j, i, g, m, h) {
					var orderId = g.data.id;
					var orderStatus = g.data.orderStatus;
					var payType = g.data.payType;
					var payStatus = g.data.payStatus;
					var orderType = g.data.orderType;
					var payAmount = g.data.payAmount;
					var k = "";
					if (orderStatus == 0 && isGranted("orderToPaid_订单详情")) {
						k += '&nbsp;&nbsp;<a href="#" onclick="lookOrderVfDetail('
								+ orderId + ')\">订单详情</a>';
					}
					return k;
				}
			}]);

	// 定义分析器
	var orderVfManagerReader = new Ext.data.JsonReader({
				totalProperty : "totalCount",
				root : "list",
				fields : [{
							name : "id"
						}, {
							name : "orderCode"
						}, {
							name : "userId"
						}, {
							name : "userName"
						},{
							name : "orderStatus"
						}, {
							name : "payStatus"
						}, {
							name : "payType"
						}, {
							name : "totalAmount"
						}, {
							name : "goodsNumber"
						}, {
							name : "payAmount"
						}, {
							name : "consignee"
						}, {
							name : "consignPhone"
						}, {
							name : "consignAddress"
						}, {
							name : "remark"
						}, {
							name : "payGoodsName"
						}, {
							name : "createdDate"
						}, {
							name : "createdName"
						}, {
							name : "cancelName"
						}, {
							name : 'orderType'
						}, {
							name : 'shipperCodeName'
						},{
							name : 'shipperPhone'
						},{ 
							name: 'shipperCode'
						}]
			});

	// 取数据store
	var orderVfViewStore = new Ext.data.Store({
				autoLoad : true,// 要加载数据必须设置为true
				proxy : new Ext.data.HttpProxy({
							url : __ctxPath + "/orderVfManager/selectOrdersVf"
						}),
				remoteSort : true,
				baseParams : {
					start : 0,
					limit : 25,
					orderStatus : '0',
					orderParam : 'id'
				},
				listeners : {
					beforeload : {
						fn : function(thiz, options) {
							Ext.apply(thiz.baseParams, Ext
											.getCmp('queryOrderVfCheckForm')
											.getForm().getValues());
						}
					}
				},
				reader : orderVfManagerReader
			});
	var orderToPaidTb = new Ext.Toolbar({// 定义grid的toolbar
		height : 25,
		items : []
	});
	orderToPaidTb.add(new Ext.Toolbar.Spacer());
	orderToPaidTb.add(new Ext.scm.Button({
				text : "刷 新",
				iconCls : "btn-refresh",
				width : 80,
				hsValue : 'orderToPaid_刷新',
				pressed : true,
				handler : function() {
					queryOrdersToPaid();
				}
			}));
	var orderToPaidManagerGrid = new Ext.grid.GridPanel({
				id : 'orderToPaidManagerGrid',
				store : orderVfViewStore,
				cm : orderVfManagerCm,
				sm : orderVfManagerSm,
				autoScroll : true,
				loadMask : true,
				stripeRows : true,
				columnLines : true,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				viewConfig : {
					emptyText : '没有您想要的数据!'
				},
				anchor : '100% 84%',
				frame : true,
				tbar : orderToPaidTb,
				bbar : new PG.PagingBar({
							id : 'orderToPaidGridPage',
							store : orderVfViewStore
						})
			});
	return orderToPaidManagerGrid;
};

// 查询
function queryOrdersToPaid() {
	PmsUtil.StringTrim("orderToPaid_Code");
	if (Ext.getCmp('queryOrderVfCheckForm').getForm().isValid()) {
		var grid = Ext.getCmp('orderToPaidManagerGrid');
		var store = grid.store;
		var pagesize = Ext.getCmp('orderToPaidGridPage').pageSize;
		store.load({
					params : {
						start : 0,
						limit : pagesize,
						"orderStatusArray" : '0'
					}
				});
	}
}

/**
 * 查询订单详情
 * 
 * @param {}
 *            id
 */
function lookOrderVfDetail(id) {
	var centerPanel = App.getContentPanel();
	var tab = centerPanel.getItem(PmsUtil.ORDER_VF_MESSAGE_DETAIL);
	var createNewTab = new CreatNewTab(PmsUtil.ORDER_VF_MESSAGE_DETAIL, '订单详情');
	if (tab == null) {
		createNewTab.createNew(tab);
	} else {
		createNewTab.createOld(tab, ['lookOrderVfDetailFields',
						'lookOrderVfDetailForm', 'lookOrderVfDetailGrid']);
	}
	Ext.Loader.load([__ctxPath + '/js/order_vf/orderVfDetail.js'], function() {
				Ext.getCmp("centerTabPanel").getActiveTab().add(
						lookOrderVfDetailSearch(id), orderVfDetailGrid(id));
				Ext.getCmp("centerTabPanel").getActiveTab().doLayout();
			});
}

Ext.getCmp("centerTabPanel").getActiveTab().add(queryOrderVfCheckSearch(),
		orderToPaidManagerGrid());
Ext.getCmp("centerTabPanel").getActiveTab().doLayout();
